﻿namespace Rio.Manager.Application.Views
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using Rio.Framework.Pages;
    using Rio.Framework.Pages.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using NetBay.Core.BusinessEntities.Connectors;
    using Rio.Manager.Application.ViewModel.Administration;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;
    using System.Collections.ObjectModel;
    using Rio.Framework.Localisation; 
    
    public partial class ConnectorAddTabView : UserControl
    {
        #region Constructors

        public ConnectorAddTabView()
        {
            InitializeComponent();

            Unloaded += (s, e) =>
            {
                Messenger.Default.Unregister<NotificationMessage<ObservableCollection<ConnectorCorrespondence>>>(this, SelectItems);
            };

            Messenger.Default.Register<NotificationMessage<ObservableCollection<ConnectorCorrespondence>>>(this, SelectItems);
        }
       
        #endregion Constructors       

        #region method

        private void SelectItems(NotificationMessage<ObservableCollection<ConnectorCorrespondence>> notification)
        {
            //Save the connector correspondence to select.
            ObservableCollection<ConnectorCorrespondence> l_oConnectorCorrespondenceToSelect = new ObservableCollection<ConnectorCorrespondence>();
            l_oConnectorCorrespondenceToSelect = (DataContext as ConnectorAddTabViewModel).ConnectorCorrespondenceListSelected;

            //Clear Selection.
            this.radGridView_Correspondences_Add.SelectedItems.Clear();

            //Selection item if necessary.
            foreach (var item in notification.Content)
            {
                //Select good item.
                var l_sConnectorIndexDescription = from ConnectorIndex in l_oConnectorCorrespondenceToSelect
                                                   where ConnectorIndex.ConnectorIndexCorrespondencePosition == item.ConnectorIndexCorrespondencePosition
                                                   select new { ConnectorIndex.ConnectorIndexCorrespondenceDescription };
                if (l_sConnectorIndexDescription.Count() != 0)
                {
                    this.radGridView_Correspondences_Add.SelectedItems.Add(item);
                }
            }
        }

        #endregion method

        #region Event

        /// <summary>Handles the SelectionChanged event of the radGridView_Correspondences_Add control.</summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Windows.Controls.SelectionChangeEventArgs" /> instance containing the event data.</param>
        public void radGridView_Correspondences_Add_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            //Setter the connector correspondence list.
            (DataContext as ConnectorAddTabViewModel).ConnectorCorrespondenceListSelected = new System.Collections.ObjectModel.ObservableCollection<ConnectorCorrespondence>(radGridView_Correspondences_Add.SelectedItems.Cast<ConnectorCorrespondence>());
        }

        /// <summary>RadGridView Validation.</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radGridView_Parameters_Add_RowValidated(object sender, Telerik.Windows.Controls.GridViewRowValidatedEventArgs e)
        {
            //RadGridView validation is ok.
            (DataContext as ConnectorAddTabViewModel).IsEditingradGridView_Parameters_Add = false;
        }

        /// <summary>RadGridView begin editing.</summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radGridView_Parameters_Add_BeginningEdit(object sender, Telerik.Windows.Controls.GridViewBeginningEditRoutedEventArgs e)
        {
            //RadGridView validation is ok.
            (DataContext as ConnectorAddTabViewModel).IsEditingradGridView_Parameters_Add = true;
        }

        #endregion Event        
    }
}